import os
from PyPDF2 import PdfFileReader, PdfFileWriter
def pdf_splitter(path,num):
    fname = os.path.splitext(os.path.basename(path))[0]
    pages = num
    pdf = PdfFileReader(path)
    pdf_writer = PdfFileWriter()
    for page in range(pdf.getNumPages()):
        pdf_writer.addPage(pdf.getPage(page))
        if (page+1) % num == 0 or page == pdf.getNumPages()-1:
            output_filename = '{}_page_{}.pdf'.format(fname, page+1)
            with open(output_filename, 'wb') as out:
                pdf_writer.write(out)
            pdf_writer = PdfFileWriter()
            print('Created: {}'.format(output_filename))
 
if __name__ == '__main__':
    path = 'example.pdf'
    num = 4
    pdf_splitter(path,num)#path是pdf路径，num隔几页分割